#ifndef XYLIB_WDF_H_20181002
#define XYLIB_WDF_H_20181002

#include <stdint.h>
#include <fstream>

class XY_Wdf{
public:
	struct WdfFileHeader {
		uint32_t		uid;
		uint32_t		offset;
		uint32_t		size;
		uint32_t		space;

		bool operator < (const WdfFileHeader& rhs) const
		{
			return uid < rhs.uid;
		}
	};

	XY_Wdf();
	~XY_Wdf();
	XY_Wdf(XY_Wdf&) = delete;
	XY_Wdf& operator=(XY_Wdf&) = delete;

	bool		Open(const char* fileName);
	bool		IsOpen(void) const;
	void		Close(void);

	const WdfFileHeader* GetInfo(uint32_t& fileCount) const
	{
		if (!IsOpen()) return nullptr;
		fileCount = _fileCount;
		return _fileList;
	}

	std::ifstream*	GetStream(void)
	{
		if (!IsOpen()) return nullptr;
		return &_file;
	}

	struct WdfFile {
		unsigned char*	pData;
		uint32_t		size;
		operator bool() const
		{
			return pData != nullptr;
		}
		WdfFile() {
			pData = nullptr;
			size = 0;
		}
		~WdfFile() {
			if (pData) {
				delete[] pData;
				pData = nullptr;
				size = 0;
			}
		}
		WdfFile(WdfFile&& r) {
			pData = nullptr;
			size = 0;
			operator=(std::move(r));
		}
		WdfFile& operator=(WdfFile&& r) {
			if (this == &r) return *this;
			if (this->pData) {
				delete[] pData;
				pData = nullptr;
				size = 0;
			}
			pData = r.pData;
			size = r.size;
			r.pData = nullptr;
			r.size = 0;
			return *this;
		}
	};
	WdfFile		GetFile(uint32_t uid);
private:
	std::ifstream		_file;
	uint32_t			_fileCount;
	WdfFileHeader*		_fileList;
};
#endif